package com.example.gzysmartcity.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.News;
import com.example.gzysmartcity.repository.model.SCNews;
import com.example.gzysmartcity.repository.model.SCNewsType;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class HomeViewModel extends ViewModel {
    private final MutableLiveData<State> stateLiveData = new MutableLiveData<>();
    //请求下来的全部新闻分类
    private List<SCNewsType.DataBean> typesList;
    //请求下来的全部新闻数据
    private List<SCNews.DataBean> allNewsList;
    //新闻数据
    private final List<News> newsList = new ArrayList<>();
    private List<Object> bannerImgUrlList;

    /**
     * 选择新闻分类
     */
    public void selectType(int p) {
        if (allNewsList == null) return;
        newsList.clear();
        int id = typesList.get(p).getId();
        allNewsList.stream().filter(a -> a.getSCNewsTypeId() == id).forEach(a -> {
            Document parse = Jsoup.parse(a.getContent());
//            LogUtils.i(parse);
            Elements imgElements = parse.getElementsByTag("img");
            String imgUrl;
            if (imgElements.size() == 0)
                imgUrl = "";
            else
                imgUrl = imgElements.get(0).attr("src");
            String content = parse.getElementsByTag("p").get(0).text();
            newsList.add(new News(imgUrl, a.getName(), content, a.getCreateTime(), a.getAuthor(), a.getContent(), a.getId()));
//            Log.i("TAG", "onSuccess: " + imgElements.size());
        });
    }


    public void getNewsType() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCNewsType>() {
            @Override
            public SCNewsType doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SCNewsType/getAll?", null, SCNewsType.class, 0);
            }

            @Override
            public void onSuccess(SCNewsType result) {
                typesList = result.getData();
                getAllNews();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                stateLiveData.postValue(State.FAIL);
            }
        });
    }

    private void getAllNews() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCNews>() {
            @Override
            public SCNews doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SCNews/getAll?", null, SCNews.class, 0);
            }

            @Override
            public void onSuccess(SCNews result) {
                if (typesList.size() == 0) return;
                if (newsList.size()>0)newsList.clear();
                int id = typesList.get(0).getId();
                allNewsList = result.getData();
                bannerImgUrlList = new ArrayList<>();
                allNewsList.stream().filter(a -> a.getSCNewsTypeId() == id).forEach(a -> {
                    Document parse = Jsoup.parse(a.getContent());
//                    LogUtils.i(parse);
                    Elements imgElements = parse.getElementsByTag("img");
                    String imgUrl;
                    if (imgElements.size() == 0)
                        imgUrl = "";
                    else
                        imgUrl = imgElements.get(0).attr("src");
                    bannerImgUrlList.add(imgUrl);
                    String content = parse.getElementsByTag("p").get(0).text();
                    newsList.add(new News(imgUrl, a.getName(), content, a.getCreateTime(), a.getAuthor(), a.getContent(), a.getId()));
//                    Log.i("TAG", "imgUrlSize: " + imgElements.size());
                });
                stateLiveData.setValue(State.SUCCESS);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                stateLiveData.setValue(State.FAIL);
            }
        });
    }

    public News getContent(int p) {
        return newsList.get(p);
    }


    public List<News> getNewsList() {
        return newsList;
    }

    public List<Object> getBannerImgUrlList() {
        return bannerImgUrlList;
    }

    public MutableLiveData<State> getStateLiveData() {
        return stateLiveData;
    }

    public List<SCNewsType.DataBean> getTypesList() {
        return typesList;
    }
}
